package org.tigris.atlas.quizme.client.action;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.tigris.atlas.quizme.client.utils.Constants;
import org.tigris.atlas.quizme.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.quizme.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.quizme.transfer.Quiz;

/**
 * Closes a Quiz by setting the end time.  This allows time calculations
 * 
 * @author Robert Simmering
 */
public class EndQuizAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		      HttpServletRequest request, HttpServletResponse response) {	
		
		Quiz quiz = (Quiz)request.getSession().getAttribute(Constants.QUIZ_KEY);
		quiz.setTimeEnded(new Timestamp(new Date().getTime()));
		
		// Use the generic EntityMaintenanceService to save the Quiz Entity
		EntityMaintenanceServiceDelegate delegate = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
		delegate.save(quiz);
		
		return mapping.findForward("success");
	}
}
